package com.dzm.portal.service.impl;

import com.dzm.portal.service.PortalService;
import com.ruoyi.common.core.constant.CacheConstants;
import com.ruoyi.common.core.constant.MQConstants;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.redis.service.RedisService;
import com.ruoyi.system.api.RemoteManagerService;
import com.ruoyi.system.api.domain.Content;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Collection;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
 * 作者：Dzm
 * 时间：2024-09-07-11:47
 * 功能：
 */

@Service
public class PortalServiceImpl implements PortalService {

    @Autowired
    private RemoteManagerService remoteManagerService;

    @Autowired
    private RedisService redisService;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    /**
     * 借助canal实现监听数据库的增删改操作，从而进行缓存操作
     * @param cid
     * @return
     */
    public List<Content> getContentsByCateId(Long cid) {
        Collection<Object> values = redisService.getCacheMap(CacheConstants.CONTENT_LIST).values();
        List<Content> list = null;

        if(!CollectionUtils.isEmpty(values)) {
            list = values.stream().map(m -> (Content) m).collect(Collectors.toList());
        }
        else {
            R<List<Content>> r = remoteManagerService.getContentsByCateId(cid);
            list = r.getData();

            //发送消息实现异步存入缓存
            for (Content content : list) {
                redisService.setCacheMapValue(CacheConstants.CONTENT_LIST, content.getId().toString(), content);
            }
        }

        return list;
    }

    @Override
    @CachePut(cacheNames = "contentList", key = "#p0")  //缓存中有无数据都执行
    public Content getContentById(Long id) {
        R<Content> r = remoteManagerService.getContentById(id);
        Content data = r.getData();
        return data;
    }

    /**
     * 借助rabbitMq实现异步缓存操作
     * @param cid
     * @return
     */
    public List<Content> getContentsByCateId4(Long cid) {
        Collection<Object> values = redisService.getCacheMap(CacheConstants.CONTENT_LIST).values();
        List<Content> list = null;

        if(!CollectionUtils.isEmpty(values)) {
            list = values.stream().map(m -> (Content) m).collect(Collectors.toList());
        }
        else {
            R<List<Content>> r = remoteManagerService.getContentsByCateId(cid);
            list = r.getData();

            //发送消息实现异步存入缓存
            for (Content content : list) {
                rabbitTemplate.convertAndSend(MQConstants.CONTENT_EXCHANGE, MQConstants.SAVE_CONTENT, content);
            }

        }

        return list;
    }

    /**
     * 使用cache注解方式存取缓存
     * @param cid
     * @return
     */
//    @Cacheable(cacheNames = "contentList", key = "#root.methodName")
    @Cacheable(cacheNames = "contentList", key = "#p0") //缓存中没数据才执行
    public List<Content> getContentsByCateId3(Long cid) {
        R<List<Content>> r = remoteManagerService.getContentsByCateId(cid);
        List<Content> list = r.getData();

        return list;
    }

    /**
     * 以Hash的方式存取缓存
     * @param cid
     * @return
     */
    public List<Content> getContentsByCateId2(Long cid) {
        Collection<Object> values = redisService.getCacheMap(CacheConstants.CONTENT_LIST).values();
        List<Content> list = null;

        if(!CollectionUtils.isEmpty(values)) {
            list = values.stream().map(m -> (Content) m).collect(Collectors.toList());
        }
        else {
            R<List<Content>> r = remoteManagerService.getContentsByCateId(cid);
            list = r.getData();

            //存入缓存
            list.forEach(content -> {
                redisService.setCacheMapValue(CacheConstants.CONTENT_LIST, content.getId().toString(), content);
            });
        }

        return list;
    }

    /**
     * 以String的方式存取缓存
     * @param cid
     * @return
     */
    public List<Content> getContentsByCateId1(Long cid) {
        List<Content> list = redisService.getCacheObject(CacheConstants.CONTENT_LIST + cid);

        if(CollectionUtils.isEmpty(list)) {
            R<List<Content>> r = remoteManagerService.getContentsByCateId(cid);
            list = r.getData();

            //存入缓存
            redisService.setCacheObject(CacheConstants.CONTENT_LIST + cid, list, 1L, TimeUnit.DAYS);
        }

        return list;
    }
}
